package fun.util.daohelper.hibernate.expression;

import fun.util.daohelper.expression.And;
import fun.util.daohelper.expression.SearchExpression;
import fun.util.daohelper.hibernate.ExpressionTransformerFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 * Author: binz
 * Date: Sep 6, 2006
 * Time: 10:40:40 AM
 */
public class AndTransformer implements ExpressionCriterionTransformer<And> {


    public Criterion transform( And and ) {
        ExpressionTransformerFactory transformerFactory = ExpressionTransformerFactory.INSTANCE;
        Criterion composite = null;
        for( SearchExpression exp : and.getExpressions() ) {
            ExpressionCriterionTransformer transformer = transformerFactory.getTransformer( exp.getClass() );
            Criterion criteria = transformer.transform( exp );

            if ( composite == null ) {
                composite = criteria;
            }
            else {
                composite = Restrictions.and( composite, criteria );
            }
        }
        return composite;
    }
}
